package com.xz.spi.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fingal on 2017-4-13.
 */
public class FilterChain implements Filter {
    List<Filter> fs = new ArrayList<Filter>();
    int index = 0;
    
    public FilterChain() {}
    
    public FilterChain(FilterChain filterChain) {
        fs = new ArrayList<Filter>(filterChain.fs);
        index = 0;
    }

    public FilterChain addFilter(Filter f) {
        fs.add(f);
        return this;
    }

    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        if (index == fs.size()) return;
        Filter f = fs.get(index);
        index++;

        f.doFilter(request, response, chain);
    }
    
    public void reset() {
        index = 0;
    }
}
